package com.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.entity.Pagination;
import com.entity.User;
import com.service.UserService;

@ParentPackage("json-default")
// @Result(type = "json", name = "add")
public class UserAction extends BaseAction {
	@Autowired
	private UserService userService;
	private User user;
	private List<User> users;
	private Pagination page;

	private int pageNum = 1;// dwz分页使用
	private int numPerPage = 20;
	private boolean ok = false;
	private String code;
	private String error;
	

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public boolean isOk() {
		return ok;
	}

	public void setOk(boolean ok) {
		this.ok = ok;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User User) {
		this.user = User;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> Users) {
		this.users = Users;
	}

	@Action(value = "show-user", results = { @Result(name = "success", location = "show-user.jsp") })
	public String user() {
		page = new Pagination(pageNum, numPerPage);
		int totalCount = userService.countAll(user);
		page.setCount(totalCount);
		users = userService.getAllUsers(page, user);
		return "success";
	}

	@Action(value = "del-user", results = { @Result(type = "json", name = "success") })
	public String delCus() throws Exception {
		userService.deleteUser(user);
		setOkJson();
		setForwardUrl("show-user.action");
		return "success";
	}

	@Action(value = "add-user", results = { @Result(type = "json", name = "success") })
	public String addCus() throws Exception {
		userService.insertUser(user);
		setOkJson();
		setForwardUrl("show-user.action");
		return "success";
	}

	@Action(value = "update-user", results = { @Result(type = "json", name = "success") })
	public String updateCus() throws Exception {
		
		userService.updateUser(user);
		setOkJson();
		setForwardUrl("show-user.action");
		return "success";
	}

	@Action(value = "find-user", results = { @Result(name = "success", location = "update-user.jsp") })
	public String findUserById() throws Exception {
		user = userService.findById(user.getId());
		return "success";
	}

	@Action(value = "login", results = {
			@Result(name = "success", location = "index.jsp"),
			@Result(name = "fail", location = "login.jsp") })
	public String login() throws Exception {
		sessionMap.remove("error");
		user = userService.findUser(user);
		String yzm = (String) sessionMap.get("yzm");
		if (user == null) {
			error="用户名或者密码错误！";
			sessionMap.put("error", error);
			return "fail";
		}
		sessionMap.remove("error");
		sessionMap.put("cuser", user);		
		return "success";
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Action(value = "checkYzm", results = { @Result(type = "json", name = "success") })
	public String checkYzm() {
		String scode = (String) sessionMap.get("yzm");
		if (scode.equals(code)) {
			ok = true;
		}
		return "success";
	}
}
